package com.foreknow.dao.Impl;

import com.foreknow.dao.BusinessDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Business;

import java.sql.SQLException;
import java.util.List;

public class BusinessDaoImpl extends BaseDAO implements BusinessDao {
    @Override
    public List<Object> queryall() throws SQLException {
        String sql = "select * from business";
        EntityMapping mapping = mappingFactory.getmap(MappingFactory.BUSINESSMAPPING);
        List<Object> list = jdbcTemplate.query(sql, mapping);
        return list;
    }

    @Override
    public List<Object> qaueryByName(String name) throws SQLException {
        String sql="select * from business where businessname=?";
        EntityMapping mapping = mappingFactory.getmap(MappingFactory.BUSINESSMAPPING);
        List<Object> list = jdbcTemplate.query(sql, mapping, name);
        return list;
    }

    @Override
    public boolean updateById(Business business, int id) throws SQLException {
        String sql="update budiness set password=?,businessName=?,businessAddress=?,bussinessExplain=?,starPrice=?,deliveryPrice=? where businessid=?";
        int update = jdbcTemplate.update(sql, business.getPassword(), business.getBusinessName(),
                business.getBusinessAddress(), business.getBussinessExplain(), business.getStarPrice(), business.getDeliveryPrice(), id);
        return update==1;
    }

    @Override
    public boolean insert(Business business) throws SQLException {
        String sql="insert into business(password,businessName,businessAddress,bussinessExplain,starPrice,deliveryPrice values(?,?,?,?,?,?))";
        int insert = jdbcTemplate.update(sql, business.getPassword(), business.getBusinessName(),
                business.getBusinessAddress(), business.getBussinessExplain(), business.getStarPrice(), business.getDeliveryPrice());
        return insert==1;
    }

    @Override
    public boolean deleteById(int id) throws SQLException {
        String sql="delete from business where businessid=?";
        int delete = jdbcTemplate.update(sql, id);
        return delete==1;
    }
}
